package com.shangs.platform.modular.cms.article.controller;

import com.shangs.platform.common.constant.factory.ConstantFactory;
import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.CmsArticleMapper;
import com.shangs.platform.common.persistence.model.CmsArticle;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.shiro.ShiroKit;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.cms.warpper.ArticleWarpper;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章管理控制器
 *
 * @author shangs
 * @Date 2017-10-25 13:06:33
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

    private String PREFIX = "/cms/article/";

    @Resource
    CmsArticleMapper cmsArticleMapper;

    /**
     * 跳转到文章管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "article.html";
    }

    /**
     * 跳转到添加文章管理
     */
    @RequestMapping("/article_add")
    public String articleAdd() {
        return PREFIX + "article_add.html";
    }

    /**
     * 跳转到修改文章
     */
    @RequestMapping("/article_update/{articleId}")
    public String articleUpdate(@PathVariable Long articleId, Model model) {
        if (ToolUtil.isEmpty(articleId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsArticle cmsArticle = this.cmsArticleMapper.selectByPrimaryKey(articleId);
        Map<String, Object> map = BeanKit.beanToMap(cmsArticle);
        map.put("catalogName", ConstantFactory.me().getCatalogNameById(cmsArticle.getCatalogId()));
        model.addAttribute("article", map);
        LogObjectHolder.me().set(map);
        return PREFIX + "article_edit.html";
    }

    /**
     * 获取文章列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String title, @RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String endTime, @RequestParam(required = false) Long catalogId) {
        List<Map<String, Object>> list = this.cmsArticleMapper.selectByParam(null, title, beginTime, endTime, catalogId);
        return new ArticleWarpper(list).warp();
    }

    /**
     * 新增文章
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsArticle article) {
        article.setContent(StringEscapeUtils.unescapeHtml(article.getContent()));
        article.setCreateId(ShiroKit.getUser().getId());
        article.setCreateTime(new Date());
        this.cmsArticleMapper.insertSelective(article);
        return SUCCESS_TIP;
    }

    /**
     * 删除文章
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long articleId) {
        this.cmsArticleMapper.deleteByPrimaryKey(articleId);
        return SUCCESS_TIP;
    }


    /**
     * 修改文章
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid CmsArticle cmsArticle, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        cmsArticle.setContent(StringEscapeUtils.unescapeHtml(cmsArticle.getContent()));
        cmsArticle.setUpdateId(ShiroKit.getUser().getId());
        cmsArticle.setUpdateTime(new Date());
        this.cmsArticleMapper.updateByPrimaryKeySelective(cmsArticle);
        return SUCCESS_TIP;
    }

    /**
     * 文章详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Long articleId) {
        if (ToolUtil.isEmpty(articleId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsArticle cmsArticle = this.cmsArticleMapper.selectByPrimaryKey(articleId);
        Map<String, Object> map = BeanKit.beanToMap(cmsArticle);
        return map;
    }

    public static void main(String[] args) {
        System.out.println(StringEscapeUtils.unescapeHtml("&lt;p&gt;123&lt;/p&gt;"));
    }
}
